#include "SceneObject.h"

SceneObject::SceneObject()
{
	coordinates = new float[3];
	color = new float[3];

	for (int i=0; i<3; i++)
	{
		coordinates[i] = 0;
		color[i] = rand() * 1.0f / RAND_MAX;
	}
}

SceneObject::SceneObject(float* coordinates, float* color)
{
	//memcpy(this->coordinates, coordinates, sizeof(float) *3);
	this->coordinates = new float[3];
	this->color = new float[3];

	for (int i=0; i<3; i++)
	{
		this->coordinates[i] = coordinates[i];
		if (!color)
		{	
			this->color[i] = rand() * 1.0f / RAND_MAX;
		}
		else
		{
			this->color[i] = color[i];
		}
	}
}

SceneObject::SceneObject(float xRange, float yRange, float zRange)
{
	coordinates = new float[3];
	color = new float[3];

	coordinates[0] = rand() * xRange / (float)RAND_MAX;
	coordinates[1] = rand() * yRange / (float)RAND_MAX;
	coordinates[2] = rand() * zRange / (float)RAND_MAX;

	for (int i=0; i<3; i++)
	{		
		color[i] = rand() * 1.0f / RAND_MAX;
	}
}

void SceneObject::draw ()
{
	/* Draw rectangle at the given position */
	glBegin(GL_QUADS);
		glColor3f(color[0], color[1], color[2]);
		glVertex3f(coordinates[0]-SQUARE_WIDTH/2.0f, 
				   coordinates[1]+SQUARE_WIDTH/2.0f, 
				   coordinates[2]);
		glVertex3f(coordinates[0]+SQUARE_WIDTH/2.0f, 
				   coordinates[1]+SQUARE_WIDTH/2.0f, 
				   coordinates[2]);
		glVertex3f(coordinates[0]+SQUARE_WIDTH/2.0f, 
				   coordinates[1]-SQUARE_WIDTH/2.0f, 
				   coordinates[2]);
		glVertex3f(coordinates[0]-SQUARE_WIDTH/2.0f, 
				   coordinates[1]-SQUARE_WIDTH/2.0f, 
				   coordinates[2]);
    glEnd();
}

void SceneObject::translate(const float* newCoordinate)
{
	for (int i=0; i<3; i++)
	{
		coordinates[i] = newCoordinate[i];
	}	
	//memcpy(coordinates, newCoordinate, 3*sizeof(float));
}

void SceneObject::colorRandomly()
{
	for(int i=0; i<3; i++)
		this->color[i] = rand() * 1.0f / RAND_MAX;
}